let app = getApp();
var siteinfo = require('../../siteinfo.js');
Page({
  data: {
    array: [ {
      id: 5,
      name: '考核点'
    }, {
      id: 2,
        name: '考评站'
    }],
    //0培训机构，1实训基地，2鉴定站，3招聘企业，4培训考试点
    index: 0,
    floorstatus: false, // 返回顶部
    list: [], //放置返回数据的数组
    no_data: false, //“没有数据”的变量，默认false，隐藏
    reachLoading: true, //"上拉加载"的变量，默认false，隐藏
    current: 1, //第几页,第几次加载
    number:10, //需要返回数据的个数
    name: '', //站点姓名
    type:5, //站点类型
    typeName:'考核点'//默认查询考点
  },

  onLoad: function(options) {

  },

  onShow: function() {
    this.getList()
  },
  //获取站点列表
  getList: function() {
    var _this = this
    _this.setData({
      current: 1,
      no_data: false
    });
    app._get(siteinfo.selExaminationPoint, {
      number: _this.data.number,
      current: _this.data.current,
      name: _this.data.name,
      type: _this.data.type
    }, function(result) {
      _this.setData({
        list: result.data.records
      });
      if ((result.data.current * result.data.size) >= result.data.total) {
        _this.setData({
          no_data: true
        });
      }
      wx.stopPullDownRefresh();
    })
  },
  //选择站点
  bindPickerChange: function(e) {
    var _this = this
    _this.setData({
      index: e.detail.value,
      type: _this.data.array[e.detail.value].id
    })
  },
  //获取站点名称
  getName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  //搜索
  search: function () {
    var _this = this
    this.setData({
      no_data: false,
      current: 1,
      number: 10,
      typeName: _this.data.array[_this.data.index].name
    });
    this.getList()
  },
  //拨打电话
  call: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.mobile,
    })
  },
  onPullDownRefresh: function () {
    var _this = this
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.searchScrollLower();
  },

  //滚动到顶部触发事件
  searchScrollToupper: function () {
    let _this = this;
    wx.showLoading({
      title: '重新加载中...',
    })
    setTimeout(function () {
      _this.data.current = 1;
      _this.data.no_data = false;
      _this.onLoad();
      wx.hideLoading();
    }, 1000);
  },
  searchScrollLower: function () {
    let _this = this;
    if (_this.data.reachLoading && !_this.data.no_data) {
      _this.setData({
        current: _this.data.current + 1, //每次触发上拉事件，把current+1
      });
      wx.showLoading({
        title: '加载中',
      })
      //网络请求
      _this.reachAppendMsgList();
    }
  },
  /**
   * 上拉追加消息列表
   */
  reachAppendMsgList: function () {
    let _this = this;
    app._get(siteinfo.selExaminationPoint, {
      number: _this.data.number,
      current: _this.data.current,
      name: _this.data.name,
      type: _this.data.type
    }, function (result) {
      //以/n把字符串进行拆分
      var list = result.data.records;
      _this.setData({
        list: _this.data.list.concat(result.data.records)
      });
      if ((result.data.current * result.data.size) >= result.data.total) {
        _this.setData({
          no_data: true
        });
      }
      wx.hideLoading();
    });
  },
  /**
   * 返回顶部
   */
  goTop: function (e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  /**
   * 显示/隐藏 返回顶部按钮
   */
  onPageScroll: function (e) {
    this.setData({
      floorstatus: e.scrollTop > 200
    })
  },

  onShareAppMessage: function() {

  }
})